package com.kly.cashmall;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.multidex.MultiDex;

import com.hjq.toast.ToastUtils;
import com.ishumei.smantifraud.SmAntiFraud;
import com.kly.cashmall.services.AppGlobalConfig;
import com.kly.cashmall.widget.refresh.RefreshInit;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshFooterCreator;
import com.scwang.smart.refresh.layout.listener.DefaultRefreshHeaderCreator;

import java.util.List;

/**
 * @author zxp
 * @since 26/10/21
 */
public class AppApplication  extends Application {

    private static AppApplication myApp = null;
    public static SharedPreferences sharedPreferences = null;

    public static final String RUNNING_DEBUG = "debug";
    public static final String RUNNING_RELEASE = "release";
    private static final String RUNNING_CUSTOM = "custom";

    public static final String RUNNING_ENVIRONMENT = RUNNING_DEBUG;

//    private static final String DEBUG_BASE_URL = "http://112.124.66.71:9104";
    private static final String DEBUG_BASE_URL = "http://192.168.5.3:9104";
    private static final String RELEASE_BASE_URL = "https://dc-api.rupee-mama.in";

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.color_f5f5f5, R.color.color_999999);//全局设置主题颜色
                return new ClassicsHeader(context).setDrawableSize(10);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(10);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();

        AppApplication.myApp = this;
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        ToastUtils.init(this);
        initSM();
        RefreshInit.init(this);
        initBaseUrl();

    }

    //初始化 设置网络请求地址
    private void initBaseUrl() {
        switch (RUNNING_ENVIRONMENT) {
            case RUNNING_DEBUG://测试环境
                AppGlobalConfig.getInstance().setBaseUrl(DEBUG_BASE_URL);
                break;
            case RUNNING_RELEASE://正式环境
                AppGlobalConfig.getInstance().setBaseUrl(RELEASE_BASE_URL);
                break;
            case RUNNING_CUSTOM://自定义开发环境 用于线路切换
                AppGlobalConfig.getInstance().setBaseUrl(getStringPreference("custom_base_url",
                        RELEASE_BASE_URL));
                break;
        }
    }

    public static AppApplication getInstance() {
        return myApp;
    }

    protected boolean isMainProcess() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {

                return true;
            }
        }
        return false;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    /**
     * 数美
     */
    private void initSM() {
        SmAntiFraud.SmOption option = new SmAntiFraud.SmOption();
        option.setOrganization("MiFOkgoS8xBGEel7vtN0");//organization 是数美分配给企业的唯一标识码
        option.setTransport(true);
        SmAntiFraud.create(this, option);
    }

    /**
     * 保存
     */
    public static void putPreference(String key, boolean value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void putIntPreference(String key, int value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putFloatPreference(String key, float value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static float getFloatPreference(String key, float defValue) {
        return sharedPreferences.getFloat(key, defValue);
    }

    public static boolean getBPreference(String key) {
        return sharedPreferences.getBoolean(key, false);
    }

    public static boolean getBPreference(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    public static void putPreference(String key, String value) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getStringPreference(String key, String defValue) {
        return sharedPreferences.getString(key, defValue);
    }

    public static int getIntPreference(String key, int defValue) {
        return sharedPreferences.getInt(key, defValue);
    }

    public static String getPreference(String key) {
        return sharedPreferences.getString(key, null);
    }

    public static void putLongPreference(String key, long time) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, time);
        editor.commit();
    }

    public static long getLongPreference(String key) {
        return sharedPreferences.getLong(key, -1);
    }
}